﻿using System;

namespace TciNewsReader_2014_04.Model
{
    class UriHelpers
    {
        public bool TryValidateUri(string url, out string result)
        {
            result = String.Empty;
            try
            {
                if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
                {
                    Uri didCreate;
                    if (Uri.TryCreate(url, UriKind.Absolute, out didCreate))
                    {
                        if (Uri.CheckSchemeName(didCreate.Scheme))
                        {
                            UriHostNameType uriHostNameType = Uri.CheckHostName(didCreate.Host);
                            if (uriHostNameType != UriHostNameType.Unknown)
                            {
                                return true;
                            }
                            result = "Invalid host type: " + uriHostNameType;
                        }
                        else
                        {
                            result = "Invalid scheme: " + didCreate.Scheme;
                        }
                    }
                    else
                    {
                        result = "Unable to create URI";
                    }
                }
                else
                {
                    result = "Invalid url string";
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return false;
        }
    }
}
